cmake_minimum_required(VERSION 3.14)
project(vehicle_model)

# Default to C++17
if(NOT CMAKE_CXX_STANDARD)
  set(CMAKE_CXX_STANDARD 17)
endif()

if(CMAKE_COMPILER_IS_GNUCXX OR CMAKE_CXX_COMPILER_ID MATCHES "Clang")
  add_compile_options(-Wall -Wextra -Wpedantic)
endif()

# Find dependencies
find_package(ament_cmake REQUIRED)
find_package(rclcpp REQUIRED)
find_package(rclcpp_components REQUIRED)
find_package(std_msgs REQUIRED)
find_package(geometry_msgs REQUIRED)
find_package(vehicle_msgs REQUIRED)
find_package(eigen3_cmake_module REQUIRED)
find_package(Eigen3 REQUIRED)
find_package(Boost REQUIRED COMPONENTS system)
find_package(common QUIET)

# Find system dependencies for backward.hpp
find_package(PkgConfig QUIET)
if(PKG_CONFIG_FOUND)
  pkg_check_modules(LIBDW QUIET libdw)
  pkg_check_modules(LIBUNWIND QUIET libunwind)
endif()


# Add common dependency if available
  get_target_property(COMMON_INCLUDE_DIRS common::hkust_pl_common INTERFACE_INCLUDE_DIRECTORIES)
  if(COMMON_INCLUDE_DIRS)
    message(STATUS "Using common include directories: ${COMMON_INCLUDE_DIRS}")
  endif()
# Include directories
include_directories(
  include
  thirdparty/odeint-v2
  ${COMMON_INCLUDE_DIRS}
)


# Vehicle model library
add_library(${PROJECT_NAME} SHARED
  src/vehicle_model.cc
  src/ctx_idm_model.cc
  src/ideal_steer_model.cc
  src/idm_model.cc
  src/controllers/ctx_idm_velocity_controller.cc
  src/controllers/idm_velocity_controller.cc
  src/controllers/pid_controller.cc
  src/controllers/pure_pursuit_controller.cc
)

target_include_directories(${PROJECT_NAME} PUBLIC
  $<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/include>
  $<INSTALL_INTERFACE:include>
  $<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/thirdparty>
  $<INSTALL_INTERFACE:include>
)

ament_target_dependencies(${PROJECT_NAME}
  rclcpp
  rclcpp_components
  std_msgs
  geometry_msgs
  vehicle_msgs
  Eigen3
)

target_link_libraries(${PROJECT_NAME}
  ${Boost_LIBRARIES}
)

# Link system libraries for backward.hpp if available
if(LIBDW_FOUND)
  target_link_libraries(${PROJECT_NAME} ${LIBDW_LIBRARIES})
  target_compile_definitions(${PROJECT_NAME} PRIVATE BACKWARD_HAS_DW=1)
endif()

if(LIBUNWIND_FOUND)
  target_link_libraries(${PROJECT_NAME} ${LIBUNWIND_LIBRARIES})
  target_compile_definitions(${PROJECT_NAME} PRIVATE BACKWARD_HAS_UNWIND=1)
else()
  # Disable backward if dependencies are not available
  target_compile_definitions(${PROJECT_NAME} PRIVATE BACKWARD_HAS_DW=0 BACKWARD_HAS_UNWIND=0)
endif()

# Link with common library if available
if(common_FOUND)
  ament_target_dependencies(${PROJECT_NAME} common)
else()
  # Try to find the common library in the workspace for development
  find_library(COMMON_LIB hkust_pl_common 
    PATHS ${CMAKE_CURRENT_SOURCE_DIR}/../../common/build 
          ${CMAKE_CURRENT_SOURCE_DIR}/../../install/common/lib
    NO_DEFAULT_PATH
  )
  if(COMMON_LIB)
    target_link_libraries(${PROJECT_NAME} ${COMMON_LIB})
    message(STATUS "Linked with common library: ${COMMON_LIB}")
  else()
    message(WARNING "Common library not found, some functionality may be limited")
  endif()
endif()

# Install library
install(TARGETS ${PROJECT_NAME}
  EXPORT export_${PROJECT_NAME}
  ARCHIVE DESTINATION lib
  LIBRARY DESTINATION lib
  RUNTIME DESTINATION bin
)

# Install headers
install(DIRECTORY include/
  DESTINATION include
)

# Install config files
install(DIRECTORY config/
  DESTINATION share/${PROJECT_NAME}/config
)

# Install third-party headers (odeint-v2)
install(DIRECTORY thirdparty/odeint-v2/
  DESTINATION include/odeint-v2
)

# Export dependencies
ament_export_targets(export_${PROJECT_NAME} HAS_LIBRARY_TARGET)
set(EXPORT_DEPENDENCIES
  rclcpp
  rclcpp_components
  std_msgs
  geometry_msgs
  vehicle_msgs
  Eigen3
)
if(common_FOUND)
  list(APPEND EXPORT_DEPENDENCIES common)
endif()
ament_export_dependencies(${EXPORT_DEPENDENCIES})

# Testing
if(BUILD_TESTING)
  find_package(ament_lint_auto REQUIRED)
  ament_lint_auto_find_test_dependencies()
endif()

ament_package()